﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the GNU General Public License version 2 (GPLv2).
// See accompanying file License.txt or copy at http://phuse.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Phuse.IO
{
    using System;

    /// <summary>Implements the common members of <see cref="ReadBuffer"/> and <see cref="WriteBuffer"/>.</summary>
    internal abstract class Buffer
    {
        private readonly byte[] buffer;
        private int count;

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal Buffer(int bufferSize)
        {
            this.buffer = new byte[bufferSize];
        }

        /// <summary>Gets or sets the byte in the buffer at <paramref name="index"/>.</summary>
        /// <exception cref="IndexOutOfRangeException"><paramref name="index"/> >= <see cref="Capacity"/>.</exception>
        internal byte this[int index]
        {
            get { return this.buffer[index]; }
            set { this.buffer[index] = value; }
        }

        /// <summary>Gets or sets the number of bytes actually contained in the buffer.</summary>
        internal int Count
        {
            get { return this.count; }
            set { this.count = value; }
        }

        /// <summary>Gets the number of bytes the buffer can contain.</summary>
        internal int Capacity
        {
            get { return this.buffer.Length; }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>Gets a reference to the internal buffer.</summary>
        protected byte[] GetBuffer()
        {
            return this.buffer;
        }
    }
}
